package be.destin.dc;

import java.util.regex.Matcher;

import be.destin.adp.Adp;
import be.destin.adp.AdpRule;
import be.destin.adp.AdpTransformation;
import be.destin.adp.AdpTransformationExec;
import be.destin.adp.AdpWIN;
import be.destin.adp.AdpWINdata;
import be.destin.adp.Rex;

public class ExtPMID extends Adp {

	static final int BlocSize = 20;
	
	static AdpTransformation extrairePubMed = new AdpTransformation ();
	static AdpTransformation numbersOnly = new AdpTransformation ();
	static AdpWINdata aWIN = new AdpWINdata(AdpWIN.WIN_switch,numbersOnly);
	//static AdpRule extPMID1_1 = new AdpRule(trfExtPMID1, concat(opts(blank),test(minmax(6,0,digit))), null, AdpWINdata.WIN_break);
	static Rex field = inexset("","|"+controlc);
	static Rex sep = quote("|");
	static String eol = "\r\n";
	static Rex pmid_head = concat(quote("p"),opt(quote("ub")),quote("m"),opt(quote("ed")),quote("id"));

	static final String lowalpha       = fromto('a','z');
	static final String hialpha        = fromto('A','Z');

	static final String alpha          = lowalpha+hialpha;
	//predefined digit = \d
	static final String alphadigit     = alpha+digit;

	static final String safe           = quote("$-_.+").toString();
	static final String extra          = quote("!*'(),").toString();
	static final String unreserved  = alpha+digit+safe+extra;

	static final Rex hex            = inset(digit.toString(), fromto('A','F') , fromto('a','f'));
	static final Rex escape         = concat(quote('%'), minmax(2,2,hex));

	static final Rex uchar          = or (inset(unreserved), escape);
	static final Rex digits         = some(digit);

	static final Rex hsegment       = opts( or(uchar , quote(';') , quote(':') , quote('@') , quote('&') , quote('=') ));
	static final Rex hpath          = concat(hsegment, opts(quote('/'), hsegment));
	static final Rex search         = hsegment;

	static final Rex domainlabel    = concat(some(inset(alphadigit)), opts(quote('-'),some(inset(alphadigit))));
	static final Rex toplabel       = concat (inset(alpha), opts(inset(alphadigit)), opts(quote('-'),some(inset(alphadigit))));
	static final Rex hostname       = concat ( opts( domainlabel, quote('.') ), toplabel);
	static final Rex hostnumber     = concat(digits,quote('.'),digits,quote('.'),digits,quote('.'),digits);
	static final Rex host           = or (hostname , hostnumber );
	static final Rex port           = digits;
	static final Rex hostport       = concat ( host, opt(quote(':'), port));
	static public final Rex httpurl = concat(quote("http://"), hostport, opt(quote('/'), hpath, opt(quote('?'),search)));


	
	static KeepPMID extPMID1_2 = new ExtPMID.KeepPMID(extrairePubMed, concat(minmax(4,4,concat(field,sep)),capt(field),sep,capt(some(digit)),opts(inexset("",eol))), "\\2\\n", AdpWINdata.WIN_continue);
	static KeepPMID extPMID1_3 = new ExtPMID.KeepPMID(extrairePubMed, concat(quote("<"),pmid_head,opts(blank),opt(quote(":"),opts(blank)),
			minmax(4,4,concat(field,sep)),capt(field),sep,capt(some(digit)),opts(inexset("",eol))), "\\2\\n", AdpWINdata.WIN_continue);

	static class KeepPMID extends AdpRule {

		public KeepPMID(AdpTransformation transformation, Rex expression,
				String replacement, AdpWINdata win) {
			super(transformation,expression,replacement,win);
		}

		public String found(AdpTransformationExec transfoExec, Matcher matcher ) {
			StringBuffer sb = new StringBuffer();//ACCUMULER LES PMID!!!
			matcher.appendReplacement(sb, "***");
			return sb.toString();
		}
		
	}

}
